package com.qianyu.versioncontrolapi.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianyu.versioncontrolapi.service.VersionConfService;
import com.qianyu.versioncontrolcore.anno.OperLogAnnotation;
import com.qianyu.versioncontrolcore.entity.base.BaseIdDTO;
import com.qianyu.versioncontrolcore.entity.base.Result;
import com.qianyu.versioncontrolcore.entity.bean.VersionConf;
import com.qianyu.versioncontrolcore.entity.dto.RegisterNumDto;
import com.qianyu.versioncontrolcore.entity.dto.VersionConfDto;
import com.qianyu.versioncontrolcore.enumerate.OperLogEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "版本控制",description = "wangzhicheng")
@RequiredArgsConstructor
@RequestMapping("/version")
public class VersionConfController {

    private final VersionConfService versionConfService;

    /**
     * 新增版本管理
     */
    @ApiOperation(value = "新增版本管理")
    @PostMapping("/save")
    Result<String> saveVersion(@RequestBody @Validated VersionConfDto.VersionConfSaveDto dto){
        versionConfService.saveVersion(dto);
        return Result.success();
    }

    /**
     * 编辑版本管理
     */
    @ApiOperation(value = "编辑版本管理")
    @PostMapping("/update")
    Result<String> updateVersion(@RequestBody @Validated VersionConfDto.VersionConfUpdateDto dto){
        versionConfService.updateVersion(dto);
        return Result.success();
    }

    /**
     * 删除版本
     */
    @ApiOperation(value = "删除版本")
    @PostMapping("/delete")
    Result<String> deleteVersion(@RequestBody @Validated BaseIdDTO dto){
        versionConfService.deleteVersion(dto);
        return Result.success();
    }

    /**
     * 获取详情
     */
    @ApiOperation(value = "获取详情")
    @PostMapping("/getOne")
    Result<VersionConf> getOne(@RequestBody @Validated BaseIdDTO dto){
        return Result.success(versionConfService.getOne(dto));
    }

    /**
     * 分页查询版本
     */
    @ApiOperation(value = "分页查询版本")
    @PostMapping("/pageList")
    Result<Page<VersionConf>> pageList(@RequestBody VersionConfDto.VersionConfPageDto dto){
        return Result.success(versionConfService.pageList(dto));
    }

    /**
     * 获取可以注册的版本
     */
    @ApiOperation(value = "获取可以注册的版本")
    @GetMapping("/getVersionList")
    @OperLogAnnotation(menuName = "注册下发",opeType= OperLogEnum.DISTRIBUTE)
    Result<String> getVersionList(){
        return Result.success(versionConfService.getVersion());
    }

    /**
     * 查询注册数量
     */
    @ApiOperation(value = "查询注册数量")
    @PostMapping("/getRegisterNum")
    Result<Integer> getRegisterNum(@RequestBody RegisterNumDto dto){
        return Result.success(versionConfService.getRegisterNum(dto));
    }

    @ApiOperation(value = "注册数量加一")
    @GetMapping("/updateRedisCache")
    Result updateRedisCache(@RequestParam String version){
       versionConfService.updateRedisCache(version);
        return Result.success();
    }


}
